package com.xysoft.framework.orm;

import java.io.Serializable;

import org.hibernate.Criteria;

import com.xysoft.framework.entity.DeletedMarkSupport;
import com.xysoft.framework.entity.PagedCollection;

public class EntityManagerImpl<T> implements EntityManager<T> {
	private EntityDao<T> entityDao;

	public void setEntityDao(EntityDao<T> entityDao) {
		this.entityDao = entityDao;
	}

	@Override
	public EntityDao<T> getEntityDao() {
		return entityDao;
	}

	@Override
	public T createNew() {
		return getEntityDao().createNew();
	}

	@Override
	public void save(T entity) {
		getEntityDao().persist(entity);
	}

	@Override
	public void delete(T entity) {
		if(entity instanceof DeletedMarkSupport) {
			((DeletedMarkSupport) entity).setDeleted(true);
			getEntityDao().persist(entity);
		} else {
			getEntityDao().delete(entity);
		}
	}

	@Override
	public T get(Serializable id) {
		return getEntityDao().get(id);
	}

	@Override
	public Criteria createQuery() {
		return getEntityDao().createQuery();
	}

	@Override
	public PagedCollection<T> findAll() {
		return new HibernatePagedCollection<T>(getEntityDao().createQuery());
	}

}
